package com.lh.web.controller.banner;

import com.lh.common.entity.admin.Banner;
import com.lh.common.entity.admin.BannerDetail;
import com.lh.common.enums.*;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import com.lh.web.controller.AbstractController;
import com.lh.web.service.serviceAdmin.banner.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FH
 * @ClassName: BannerAction
 * @Description: (banner图管理)
 * @date: 2020-05-08 16:56
 */
@Api(value="BannerAction",tags={"banner图管理"})
@RestController
@RequestMapping("banner")
public class BannerAction extends AbstractController {

    @Autowired
    private BannerService bannerService;

    /**
     * 查询所有的banner图
     */
    @ApiOperation(value = "查询所有的banner图")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "bannerName", value = "广告名称", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "bannerStatus", value = "状态 UpperShelf:上架  LowerShelf:下架", required = false, dataTypeClass= UpperLowerShelfStatus.class),
            @ApiImplicitParam(paramType="query", name = "bannerPosition", value = "所属广告位 HOME:首页 ONEDOLLARRUSH :一元抢购", required = false, dataTypeClass= PositionTypeEnum.class)
    })
    @GetMapping("/getBannerList")
    @RequiresPermissions("banner:getBannerList")
    public Result<Banner> getBannerList(String bannerName, PositionTypeEnum bannerPosition, UpperLowerShelfStatus bannerStatus, Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("bannerName", bannerName);
        if(bannerPosition != null) {
            params.put("bannerPosition", bannerPosition.getValue());
        }
        if(bannerStatus != null) {
            params.put("bannerStatus", bannerStatus.getValue());
        }
        PageUtils pageUtils = bannerService.queryBannerList(params, pageNum, pageSize);
        return Result.ok(pageUtils);
    }

    @ApiOperation("查看banner详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "bannerId", value = "bannerId", required = true,dataTypeClass= Long.class),
            @ApiImplicitParam(paramType="query", name = "bannerShopType", value = "商品分类 ONE:一元抢购 SHOP:普通商品",dataTypeClass= BannerShopTypeEnum.class),
            @ApiImplicitParam(paramType="query", name = "bannerType", value = "跳转类型 LINK:链接 SHOP:商品",dataTypeClass= BannerTypeEnum.class)
    })
    @GetMapping("/detail")
    @RequiresPermissions("sys:banner:detail")
    public Result<BannerDetail> queryBannerDetail(Long bannerId, BannerShopTypeEnum bannerShopType,BannerTypeEnum bannerType){
        String type = "",banner = "";
        if(bannerShopType != null){
            type = bannerShopType.getValue();
        }
        if(bannerType != null){
            banner = bannerType.getValue();
        }
        BannerDetail maps = bannerService.queryBannerById(bannerId, type,banner);
        return Result.ok(maps);
    }

    /**
     * 新增banner图
     */
    @ApiOperation(value = "新增/修改banner图")
    @PostMapping("/addBanner")
    @RequiresPermissions("banner:addBanner")
    public Result<BannerDetail> addBanner(@RequestBody BannerDetail banner) {
        try {
            banner.setUpdateTime(new Date());
            if(banner.getBid() == null) {
                banner.setCreateTime(new Date());
                banner.setBannerStatus(UpperLowerShelfStatus.LOWERSHELF);
                banner.setIsDel(IsDel.NO);
                bannerService.insertBanner(banner);
                return Result.ok(banner);
            }
            bannerService.updateBanner(banner);
            return Result.ok(banner);
        } catch (Exception e) {
            return Result.error("新增/修改banner图是败!");
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除banner图")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "bannerIds", value = "banner多个ID,逗号隔开", required = true)
    })
    @GetMapping("/deleteById")
    @ResponseBody
    @RequiresPermissions("banner:deleteById")
    public Result del(Long[] bannerIds){
        try {
            bannerService.deleteBanner(bannerIds);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("删除banner图异常!");
        }
    }

    @ApiOperation(value = "banner图上下架/排序")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "bid", value = "bid", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "bannerSort", value = "排序", required = false, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "bannerPosition", value = "所属广告位", required = true, dataTypeClass = PositionTypeEnum.class),
            @ApiImplicitParam(paramType="query",name = "bannerStatus", value = "状态", required = false, dataTypeClass = UpperLowerShelfStatus.class)
    })
    @ResponseBody
    @PostMapping("/updateBannerStatus")
    @RequiresPermissions("banner:updateBannerStatus")
    public Result updateBannerStatus(@RequestBody Banner banner) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("bid",banner.getBid());
        if(banner.getBannerSort() != null) {
            params.put("bannerSort",banner.getBannerSort().toString());
        }
        if(banner.getBannerStatus() != null) {
            if(banner.getBannerStatus().equals(UpperLowerShelfStatus.UPPERSHELF)){
                Banner byId = bannerService.getById(banner.getBid());
                if(byId.getBannerType().equals(BannerTypeEnum.SHOP)){
                    Map<String, Object> map = bannerService.queryShopStatus(byId.getBannerShopType().getValue(), byId.getSid());
                    AssertUtil.assertNotEquals(map.get("status").toString(),UpperLowerShelfStatus.LOWERSHELF.getValue(), Constant.RESULT.FI1012,"活动或商品已被下架,不可显示,请重新关联活动!");
                    if(byId.getBannerShopType().equals(BannerShopTypeEnum.ONE)){
                        AssertUtil.assertNotEquals(map.get("activity_status").toString(),ActivityStatusEnum.NOTSTARTED.getValue(), Constant.RESULT.FI1012,"活动未开始,不可显示!");
                    }
                }
                if(banner.getBannerPosition().equals(PositionTypeEnum.ONEDOLLARRUSH)){
                    int i = bannerService.selectBanner();
                    if(i > 0){
                        return Result.error("只能有一个广告是显示状态");
                    }
                }
            }
            params.put("status",banner.getBannerStatus().getValue());
        }
        bannerService.updateBannerStatus(params);
        return Result.ok(banner.getBid());
    }
}
